/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.cms.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.cms.entity.Record_task;
import com.thinkgem.jeesite.modules.cms.entity.TaskGroup;
import com.thinkgem.jeesite.modules.cms.entity.TaskOperation;
import com.thinkgem.jeesite.modules.cms.entity.taskGroupData;
import com.thinkgem.jeesite.modules.cms.dao.TaskGroupDao;
import com.thinkgem.jeesite.modules.cms.dao.TaskOperationDao;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

// TODO: Auto-generated Javadoc
/**
 * 合并任务描述Service.
 *
 * @author leo
 * @version 2017-02-20
 */
@Service
@Transactional(readOnly = true)
public class TaskGroupService extends CrudService<TaskGroupDao, TaskGroup> {
	
	@Autowired
	private RecordService_task record_taskService;
	@Autowired
	private TaskOperationDao taskoprdao ;
	/* (non-Javadoc)
	 * @see com.thinkgem.jeesite.common.service.CrudService#get(java.lang.String)
	 */
	public TaskGroup get(String id) {
		return super.get(id);
	}
	
	/* (non-Javadoc)
	 * @see com.thinkgem.jeesite.common.service.CrudService#findList(com.thinkgem.jeesite.common.persistence.DataEntity)
	 */
	public List<TaskGroup> findList(TaskGroup taskGroup) {
		return super.findList(taskGroup);
	}
	
	/* (non-Javadoc)
	 * @see com.thinkgem.jeesite.common.service.CrudService#findPage(com.thinkgem.jeesite.common.persistence.Page, com.thinkgem.jeesite.common.persistence.DataEntity)
	 */
	public Page<TaskGroup> findPage(Page<TaskGroup> page, TaskGroup taskGroup) {
		return super.findPage(page, taskGroup);
	}
	
	/* (non-Javadoc)
	 * @see com.thinkgem.jeesite.common.service.CrudService#save(com.thinkgem.jeesite.common.persistence.DataEntity)
	 */
	@Transactional(readOnly = false)
	public void save(TaskGroup taskGroup) {
		super.save(taskGroup);
		taskoprdao.deletegroupid(taskGroup.getId());
	}
	
	/* (non-Javadoc)
	 * @see com.thinkgem.jeesite.common.service.CrudService#delete(com.thinkgem.jeesite.common.persistence.DataEntity)
	 */
	
	@Transactional(readOnly = false)
	public void delete(TaskGroup taskGroup) {
		super.delete(taskGroup);
		//删除子任务合并id
		//
	}
	
	/**
	 * Save list.
	 *
	 * @param list the list
	 * @return the int
	 */
	@Transactional(readOnly = false)
	public int saveList(List<TaskOperation> list,String userid) {
		TaskGroup tg = new TaskGroup();
		tg.setOpdesc(list.get(0).getOpdesc());
		tg.setUserid(userid);
		tg.setAllcount(list.size());
		String dptasks = "";
		String jcdeptid= list.get(0).getJcdept();
		for (TaskOperation taskOperation : list) {
			if (dptasks.contains(taskOperation.getDptask()) == false)
			{
				
				dptasks = dptasks +","+taskOperation.getDptask();
			}
		}
		tg.setJcdeptid(jcdeptid);
		tg.setAlltaskdesc(dptasks.length()>0?dptasks.substring(1):dptasks);
		tg.setIsstart(false);
		tg.setIstaskcomplete(false);	
		tg.setPdate(list.get(0).getPdate());
		tg.preInsert();
		int id = ((TaskGroupDao)(this.dao)).insertAndGetId(tg);
		
//		if (entity.getIsNewRecord()){
//			entity.preInsert();
//			dao.insert(entity);
//		}else{
//			entity.preUpdate();
//			dao.update(entity);
//		}
		
		return Integer.valueOf(tg.getId());
	}
	
	@Transactional(readOnly = false)
	public List<TaskGroup>  getByDateToPrint(String date1 ,String date2){
		List<TaskGroup> list  = ((TaskGroupDao)(this.dao)).getByDateToPrint(date1 , date2);
		
		return list;
	}
	
	@Transactional(readOnly = false)
	public taskGroupData getById(String id) {
		taskGroupData tgdata =  ((TaskGroupDao)(this.dao)).getById(id);
		return tgdata;
	}
	
	@Transactional(readOnly = false)
	public taskGroupData getById(String id,String userid) {
		taskGroupData tgdata =  ((TaskGroupDao)(this.dao)).getByIdAndUser(id,userid);
		return tgdata;
	}
	@Transactional(readOnly = false)
	public taskGroupData getByIdLimtStrat(String id) {
		taskGroupData tgdata =  ((TaskGroupDao)(this.dao)).getById(id);
		return tgdata;
	}
	
	@Transactional(readOnly = false)
	private void inittgdata(taskGroupData tgdata, TaskGroup tg) {
		tgdata.setId(Integer.valueOf(tg.getId()));
		tgdata.setAllcount(tg.getAllcount());
		tgdata.setAlltaskdesc(tg.getAlltaskdesc());
		tgdata.setIsstart(tg.getIsstart());
		tgdata.setIstaskcomplete(tg.getIstaskcomplete());
		tgdata.setOpdesc(tg.getOpdesc());
		tgdata.setPdate(tg.getPdate().toString());
		tgdata.setUserid(tg.getUserid());
	}
	
	@Transactional(readOnly = false)
	public void start(String id,String userid)
	{
		 ((TaskGroupDao)(this.dao)).updateIsStart(id, 1, userid);
		 //taskoprdao.updateGroupStart(id, 1, userid);
		 List<TaskOperation> list = taskoprdao.getupdateGroupStart(id,userid);
		 for (TaskOperation t : list){
			 String keyfield=t.getKeyfield();
			 //((TaskGroupDao)(this.dao)).updateIsStart(keyfield,1, userid);
			 taskoprdao.updateIsStart(keyfield,1, userid);
			 Record_task record = new Record_task();
				Date date = new Date();

				record.setKeyfield(keyfield);
				record.setDate(date);
				record.setTime(date);
				record.setType("S");
				record.setUserid(userid);
				record.setDescription(userid + "-开工");
				this.record_taskService.save(record);

		 }
		
	}
	public int getstart(String id,String userid)
	{
		return taskoprdao.getgroupstart(id, userid);
	}
	
	@Transactional(readOnly = false)
	public void complete(String id,String userid)
	{
		
		 //taskoprdao.updateGroupComplete(id, 1, userid);
		
		 List<TaskOperation> list = taskoprdao.getIsComplbygroup(id,userid);
		 for (TaskOperation t : list){
			 String keyfield=t.getKeyfield();
			 taskoprdao.updateIsTaskComplete(keyfield,1, userid);
			 Record_task record = new Record_task();
				Date date = new Date();

				record.setKeyfield(keyfield);
				record.setDate(date);
				record.setTime(date);
				record.setType("E");
				record.setUserid(userid);
				record.setDescription(userid + "-完工");
				this.record_taskService.save(record);

		 }
		 if (taskoprdao.checkIsComplbygroup(id,userid) <= 0){
			 ((TaskGroupDao)(this.dao)).updateIscomplete(id, 1, userid);
		 }
	}
	public int getcomplete(String id,String userid)
	{
		return taskoprdao.getgroupcomplete(id, userid);
	}

	public List<String> checkIsok(String id, String userid) {
		return taskoprdao.checkIsokbygroup(id, userid);
	}
}